package com.zrsx.searchmodel.mapper;

import com.zrsx.searchmodel.common.dto.RecordDTO;
import com.zrsx.searchmodel.common.entity.Record;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RecordMapper {

    /**
     * 计算用户累计金额
     * @param userId
     * @return
     */
    @Select("select sum(amount) from credit_contract_record where user_id = #{userId}")
    Double selectTotalAmount(Integer userId);

    /**
     * 计算用户某一服务使用次数
     * @param userId
     * @return
     */
    Integer selectUseCount(Integer userId,String recordType);

    // 总数查询（分页用）
    long count(@Param("dto") RecordDTO recordDTO);

    // 分页+条件查询列表
    List<Record> list(@Param("dto") RecordDTO recordDTO);

    //查询最近的num条Record数据
    @Select("select * from credit_contract_record where user_id = #{userId} order by finish_time desc limit #{num}")
    List<Record> listByNum(Integer userId,int num);

    //根据类型查询用户记录数
    @Select("select count(*) from credit_contract_record where user_id = #{userId} and record_type = #{recordType}")
    Integer countByType(Integer userId, String recordType);
}
